package com.lora.service.resolver;

import com.lora.dto.EquipmentDto;
import com.lora.entity.FreshAirContolAirConditioning;
import com.lora.service.FreshAirContolAirConditioningService;
import com.lora.service.NodeDataResolver;
import com.lora.util.StringUtil;
import com.lora.web.BaseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 新风控制（空调）
 */
@Service("freshAirContolAirConditioningResolver")
public class FreshAirContolAirConditioningResolver  implements NodeDataResolver<FreshAirContolAirConditioning> {
    @Resource
    private FreshAirContolAirConditioningService freshAirContolAirConditioningService;

    /**
     * 字节索引0: 设备类别，0X02(中央空调控制器) ；
     * 字节索引1: 软硬件版本，0X11(软件版本V1.0,硬件版V1.0) ；
     * 字节索引2: 数据长度；
     * 字节索引3: 消息类型：0X01(心跳主动上传数据)；0X02(读取空调状态返回数据)；
     * 0X03(设置空调状态返回数据)；
     * 字节索引4: 设置是否成功标志位，空调启停状态，模式，风速，开关机状态；
     * 字节索引5: 空调使用状态，锁定状态，温度内探头状态，当前温度小数部分；
     * 字节索引6: 为空调当前温度整数部分；
     * 字节索引7: 为空调设定温度整数部分；
     * 字节索引8: 同步时间请求标志,0x01 为请求下发同步时间，0x00 为不请求下发同步间
     * @param equipmentNumber
     * @param data
     * @return
     */
    @Override
    public EquipmentDto<FreshAirContolAirConditioning> resolve(String equipmentNumber, String data) {
        data = BaseUtil.base64ToHex(data);
        EquipmentDto<FreshAirContolAirConditioning> equipmentData = new EquipmentDto<>();
        FreshAirContolAirConditioning fresh=new FreshAirContolAirConditioning();
        fresh.setEquipmentid(equipmentNumber);
        fresh.setEquipmenttype(data.substring(0,2));
        fresh.setSoftwareversion(data.substring(2,3)+".0");
        fresh.setHardwareversion(data.substring(3,4)+".0");
        fresh.setDatalength(data.substring(4,6));
        fresh.setMessagetype(data.substring(6,8));

        String airstate=StringUtil.hexStringToByte(data.substring(8,10));
        fresh.setIsconfig(airstate.substring(0,1));
        fresh.setCommitmentstate(airstate.substring(1,2));
        fresh.setPattern(airstate.substring(2,4));
        fresh.setWindspeed(airstate.substring(4,6));
        fresh.setPanelstate(airstate.substring(6));

        String bit5=StringUtil.hexStringToByte(data.substring(10,12));
        fresh.setUsestate(bit5.substring(0,1));
        fresh.setLockstate(bit5.substring(1,2));
        fresh.setTemperatureprobestate(bit5.substring(2,4));
        fresh.setTemperaturedecimal(String.valueOf(StringUtil.hexStringToInt(bit5.substring(4))));

        fresh.setTemperatureinteger(String.valueOf(StringUtil.hexStringToInt(data.substring(12,14))));
        fresh.setTemperatureprobestate(String.valueOf(StringUtil.hexStringToInt(data.substring(14,16))));
        fresh.setRequestflag(data.substring(16));
        equipmentData.setEntity(fresh);
        return equipmentData;
    }

    @Override
    public FreshAirContolAirConditioning save(FreshAirContolAirConditioning equipment, boolean alarmFlag) {
        equipment.setAlarmFlag(alarmFlag);
        freshAirContolAirConditioningService.save(equipment);
        return equipment;
    }
}
